package com.app.fenak.asynctasks;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.app.fenak.EmailActivity;
import com.app.fenak.ForgetPasswordActivity;
import com.app.fenak.validation.ServicesUrls;

import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

public class ForgetPasswordAsyncTask extends AsyncTask<Object, Object, Boolean> {

	private ForgetPasswordActivity forgetPasswordActivity ;
	
	@Override
	protected Boolean doInBackground(Object... params) {
		
		forgetPasswordActivity = (ForgetPasswordActivity) params[0] ;
		String email = (String) params[1];
		boolean mailSent = false ;
		
		try {

			ArrayList<NameValuePair> requestString = new ArrayList<NameValuePair>();
			requestString.add(new BasicNameValuePair(
					ServicesUrls.FORGET_PASSWORD_PARAM_EMAIL, email));

			HttpClient httpClient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(ServicesUrls.FORGET_PASSWORD_URL);
			httpPost.setEntity(new UrlEncodedFormEntity(requestString));
			Log.i("request ", httpPost.getRequestLine().toString());

			HttpResponse response = httpClient.execute(httpPost);
			Log.i("response", response.getStatusLine().toString());
			String resonseString = ServicesUrls.convertStreamToString(response
					.getEntity().getContent());
			Log.i("response ", resonseString);
			JSONObject jsonObject = new JSONObject(resonseString);
			mailSent = jsonObject.getBoolean("post");
			Log.i(" email exists", mailSent + "");

		} catch (UnsupportedEncodingException e) {

			Log.e("UnsupportedEncodingException", e.getMessage());
		} catch (IOException e) {

			Log.e("IOException", e.getMessage());

		} catch (JSONException e) {

			Log.e("JSONException", e.getMessage());
		}
		return mailSent;
	}
	
	@Override
	protected void onPostExecute(Boolean mailSent) {
		
		super.onPostExecute(mailSent);
		forgetPasswordActivity.dismissProgressDialog();
		
		Intent intent ;
		
		if (mailSent) {
			
			Toast.makeText(forgetPasswordActivity,
					"Mail Sent",
					Toast.LENGTH_SHORT).show();
			intent = new Intent(forgetPasswordActivity.getApplicationContext(),
					EmailActivity.class);
			forgetPasswordActivity.startActivity(intent);
			
			
		}else {
			Toast.makeText(forgetPasswordActivity,
					"Mail Not Sent",
					Toast.LENGTH_SHORT).show();
			intent = new Intent(forgetPasswordActivity.getApplicationContext(),
					EmailActivity.class);
			forgetPasswordActivity.startActivity(intent);
		}
		
		
	}

}
